package com.mallsystem.orderservice.controller;

import com.github.pagehelper.PageInfo;
import com.mallsystem.common.bean.Order;
import com.mallsystem.common.bean.OrderVo;
import com.mallsystem.common.bean.RespBean;
import com.mallsystem.common.exception.AllException;
import com.mallsystem.common.service.OrderService;
import com.mallsystem.common.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    //TODO 快递单号 - 支付成功后添加快递订单
    //TODO 申请售后
    @PostMapping
    public RespBean insert(@RequestBody OrderVo orderVo,@RequestHeader("token") String token) throws AllException {
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");
        orderVo.setUserId(userId);
        //添加订单
        orderService.insert(orderVo);
        return RespBean.ok("添加订单成功",orderVo);
    }
    //修改
    @PutMapping
    public RespBean update(@RequestBody Order order) throws AllException {
        orderService.update(order);
        return RespBean.ok("修改成功！");
    }
    //分页搜索
    @GetMapping
    public RespBean selectByPage(Order condition, Integer pageNum, Integer pageSize,@RequestHeader("token") String token){
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 1 : pageSize;
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        String role = (String) map.get("role");
        System.out.println(role);
        if(role.equals("admin")){
            PageInfo<Order> pageInfo = orderService.selectByCondition(condition, pageNum, pageSize);
            return RespBean.ok("查询成功",pageInfo);
        }else {
            Integer userId = (Integer) map.get("id");
            condition.setUserId(userId);
            PageInfo<Order> pageInfo = orderService.selectByCondition(condition, pageNum, pageSize);
            return RespBean.ok("查询成功",pageInfo);
        }
    }
    //根据id查询
    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") String  id,@RequestHeader("token") String token) throws AllException {
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        String  role = (String) map.get("role");
        if(role.equals("user")){
            Integer userId = (Integer) map.get("id");
            Order order = orderService.selectById(id,userId);
            return RespBean.ok("查询成功",order);
        }else {
            Order order = orderService.selectByIdOnAdmin(id);
            return RespBean.ok("管理员查询成功",order);
        }

    }
    @PostMapping("/pay")
    public RespBean pay(@RequestBody OrderVo orderVo,@RequestHeader("token") String token) throws AllException {
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");
        orderVo.setUserId(userId);
        orderService.pay(orderVo);
        return RespBean.ok("支付成功");
    }
}
